package com.kaizige.realtor.Controller;

import com.kaizige.realtor.Dao.houseOwnerDao;
import com.kaizige.realtor.Dao.managerDao;
import com.kaizige.realtor.Dao.tenantDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class profileController {
	@Autowired
	houseOwnerDao hod;
	@Autowired
	managerDao md;
	@Autowired
	tenantDao td;
	@GetMapping("/ownProfile")
	public String toOwnProfile(HttpSession session, Model model){
		Map<String, Object> stringObjectMap = hod.queryByName((String) session.getAttribute("LoginUser"));
		model.addAttribute("prof",stringObjectMap);
		return "/profile/ownProfile";
	}
	
	@PostMapping("/ownProfile")
	public String ownProfile(@RequestParam("name")String name, @RequestParam("address")String address,
	                         @RequestParam("phone")String phone){
		hod.updateOwnByName(name,address,phone);
		return "redirect:/ownProfile";
	}
	
	@GetMapping("/manProfile")
	public String toManProfile(HttpSession session, Model model){
		Map<String, Object> stringObjectMap = md.queryByName((String) session.getAttribute("LoginUser"));
		model.addAttribute("prof",stringObjectMap);
		return "/profile/manProfile";
	}
	
	@GetMapping("/tenProfile")
	public String toTenProfile(HttpSession session, Model model){
		Map<String, Object> stringObjectMap = td.queryByName((String) session.getAttribute("LoginUser"));
		model.addAttribute("prof",stringObjectMap);
		return "/profile/tenProfile";
	}
	
	@PostMapping("/tenProfile")
	public String tenProfile(@RequestParam("name")String name, @RequestParam("address")String address,
	                         @RequestParam("phone")String phone){
		hod.updateOwnByName(name,address,phone);
		return "redirect:/tenProfile";
	}
}
